/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.pub.dao;

import static cn.org.rapid_framework.util.ObjectUtils.isNotEmpty;

import java.util.HashMap;
import java.util.Map;

import javacommon.base.BaseHibernateDao;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.pub.model.PubInfo;
import com.tech15.pub.vo.query.PubInfoQuery;

@Repository
public class PubInfoDao extends BaseHibernateDao<PubInfo,java.lang.String>{

	public Class getEntityClass() {
		return PubInfo.class;
	}
	
	public Page findByPageRequest(PageRequest<Map> pageRequest) {
		//XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
		// [column]为字符串拼接, {column}为使用占位符. 以下为图方便采用sql拼接,适用性能要求不高的应用,使用占位符方式可以优化性能. 
		// [column] 为PageRequest.getFilters()中的key
		String sql = "select t from PubInfo t where 1=1 "
				+ "/~ and t.zt = '[zt]' ~/"
				+ "/~ and t.lb = '[lb]' ~/"
				+ "/~ and t.nr = '[nr]' ~/"
				+ "/~ and t.fbsj = '[fbsj]' ~/"
				+ "/~ and t.fbbm = '[fbbm]' ~/"
				+ "/~ and t.fbr = '[fbr]' ~/"
				+ "/~ order by [sortColumns] ~/";
		return pageQuery(sql,pageRequest);
	}
	
	public Page findPage(PubInfoQuery query) {
		String sql = "select t.id,t.bt,t.zt,t.lb,t.fbsj,t.fbbm,t.fbr,t.istop,t.sqtop,t.topsj,t1.name as dataname,t2.name as categoryName,t2.realname from Pub_Info t " +
				" left join pub_data t1 on t.zt=t1.id " +
				" left join pub_data t2 on t.lb=t2.id " +
				" left join sys_user t2 on t.fbr=t2.userid where 1=1 ";
		
			/*if(isNotEmpty(query.getUserid())){
				sql += " and (t.isauth=0 or (select count(*) from pub_info_auth a where a.poid=t.id and a.userid="+query.getUserid()+") >0) ";
			}*/
			
			sql+= "/~ and t.bt like '%[bt]%' ~/"
			+ "/~ and t.zt = [zt] ~/"
			+ "/~ and t.lb = [lb] ~/"
			+ "/~ and t.fbsj = '[fbsj]' ~/"
			+ "/~ and t.fbbm = '[fbbm]' ~/"
			+ "/~ and t.fbr = '[fbr]' ~/"
			+ "/~ and t.istop = [istop] ~/"
			+ "/~ order by [sortColumns] ~/";
		Map map = new HashMap();
		map.put("id","String");
		map.put("bt","String");
		map.put("zt","Long");
		map.put("lb","Long");
		map.put("fbsj","Timestamp");
		map.put("fbbm","String");
		map.put("fbr","Long");
		map.put("istop","Integer");
		map.put("dataname","String");
		map.put("categoryName","String");
		map.put("realname","String");
		map.put("sqtop", "Integer");
		map.put("topsj", "Integer");
		
		return pageSqlQuery(sql, query, map, PubInfo.class);
	}
	
	public Page findPageGzzd(PubInfoQuery query) {
		String sql = "select t.id,t.bt,t.zt,t.lb,t.fbsj,t.fbbm,t.fbr,t.istop,t1.name as dataname,t2.name as categoryName,t2.realname from Pub_Info t " +
				" left join pub_data t1 on t.zt=t1.id " +
				" left join pub_data t2 on t.lb=t2.id " +
				" left join sys_user t2 on t.fbr=t2.userid where 1=1 ";
		if(ObjectUtils.isEmpty(query.getZt())){
			sql += " and zt in (select a.id from pub_data a where a.pid=58) ";
		}
			sql += "/~ and t.bt like '%[bt]%' ~/"
			+ "/~ and t.zt = [zt] ~/"
			+ "/~ and t.lb = [lb] ~/"
			+ "/~ and t.fbsj = '[fbsj]' ~/"
			+ "/~ and t.fbbm = '[fbbm]' ~/"
			+ "/~ and t.fbr = '[fbr]' ~/"
			+ "/~ and t.istop = [istop] ~/"
			+ "/~ order by [sortColumns] ~/";
		Map map = new HashMap();
		map.put("id","String");
		map.put("bt","String");
		map.put("zt","Long");
		map.put("lb","Long");
		map.put("fbsj","Timestamp");
		map.put("fbbm","String");
		map.put("fbr","Long");
		map.put("istop","Integer");
		map.put("dataname","String");
		map.put("categoryName","String");
		map.put("realname","String");
		
		return pageSqlQuery(sql, query, map, PubInfo.class);
	}
	

}
